using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using EnhancedTimer.Resources;
using Services.Menu;


namespace EnhancedTimer
{
    
    public enum TimerStatus
    {
        RunningUp,
        RunningDown,
        Paused,
        Stopped,
        Idle,
        Fired
    }
    
    class TimerMenu : DrawableGameComponent
    {
        TimeSpan timeSpan;
        TimeSpan originalTimeSpan;
        TimerStatus timerStatus = TimerStatus.Idle;
        TimerStatus originalTimerStatus;
        GraphicsDeviceManager GraphicsDeviceManager;
        InputManager input_manager;
        MyButton settingsButton, creditsButton, newTimerButton, pauseButton, resumeButton, stopButton, startTimerButton;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        protected float press_wait;
        protected readonly float PRESS_INTERVAL = 0.2f;
        MyButton simpleClock, title;
        float NOTIFY_INTERVAL = 2;
        MyButton add1minute, add5minute, add10minute, ignore, fired;
        public TimerMenu(Game game, GraphicsDeviceManager graphics)
            : base(game)
        {
            this.GraphicsDeviceManager = graphics;
            originalTimerStatus = timerStatus = TimerStatus.Idle;
            graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
            graphics.ApplyChanges();
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            //originalTimerStatus = timerStatus = TimerStatus.Idle;
            base.Initialize();
        }
        public void Resume(bool newTimer)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;

            timerStatus = TimerStatus.Idle;
            
        }
        protected override void LoadContent()
        {
            
            var font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            var titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            var box = Game.Content.Load<Texture2D>("bar_box");
            resumeButton = new MyButton(new Rectangle(40, 600, 180, 80), Strings.Resume, Color.White, font, TextPos.Center, box, Color.Yellow);
            pauseButton = new MyButton(resumeButton.rectangle, Strings.Pause, Color.White, font, TextPos.Center, box, Color.Yellow);
            startTimerButton = new MyButton(resumeButton.rectangle, Strings.Start, Color.White, font, TextPos.Center, box, Color.Green);
            stopButton = new MyButton(new Rectangle(260, 600, 180, 80), Strings.Stop, Color.White, font, TextPos.Center, box, Color.Red);
            newTimerButton = new MyButton(stopButton.rectangle, Strings.NewTimer, Color.White, font, TextPos.Center, box, Color.Green);
            creditsButton = new MyButton(new Rectangle(40, 700, 180, 80), Strings.Credits, Color.White, font, TextPos.Center, box, Color.White);
            settingsButton = new MyButton(new Rectangle(260, 700, 180, 80), Strings.Settings, Color.White, font, TextPos.Center, box, Color.White);
            title = new MyButton(new Rectangle(0, 0, 480, 150), "Enhanced Timer", Color.White, titleFont);
            simpleClock = new MyButton(new Rectangle(10, 400, 460, 200), "", Color.White, titleFont);

            fired = new MyButton(new Rectangle(0, 150, 480, 100), Strings.Fired, Color.White, titleFont);
            add1minute = new MyButton(new Rectangle(0, 300, 480, 100), Strings.Add1Minute, Color.White, font, TextPos.Center, box, Color.White);
            add5minute = new MyButton(new Rectangle(0, 420, 480, 100), Strings.Add5Minute, Color.White, font, TextPos.Center, box, Color.White);
            add10minute = new MyButton(new Rectangle(0, 540, 480, 100), Strings.Add10Minute, Color.White, font, TextPos.Center, box, Color.White);

            ignore = new MyButton(new Rectangle(0, 660, 480, 100), Strings.Stop, Color.White, font, TextPos.Center, box, Color.White);

            resumeButton.enabled = false;
            pauseButton.enabled = false;
            stopButton.enabled = false;
            newTimerButton.enabled = true;
            startTimerButton.enabled = false;
            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }

        public void SetNewTimer(TimeSpan timeSpan, TimerStatus status)
        {
            originalTimerStatus = status;
            this.timeSpan = timeSpan;
            originalTimeSpan = timeSpan;
            startTimerButton.enabled = true;
            resumeButton.enabled = false;
            pauseButton.enabled = false;
            stopButton.enabled = false;
        }
        float time_from_last_notify = 0;
        public override void Update(GameTime gameTime)
        {
            var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (press_wait < PRESS_INTERVAL)
                press_wait += dt;
            if (timerStatus == TimerStatus.Fired)
            {
                time_from_last_notify += dt;
                if (time_from_last_notify >= NOTIFY_INTERVAL)
                {
                    time_from_last_notify = 0;
                    if (StateManager.OptionsState.sound)
                        StateManager.Sounds[StateManager.OptionsState.alertSound].Play();
                    if (StateManager.OptionsState.vibration)
                        Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 1));
                }

            }
            {
                if (timerStatus == TimerStatus.RunningDown || timerStatus == TimerStatus.RunningUp)
                {

                    //var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (timerStatus == TimerStatus.RunningUp)
                        dt = -dt;
                    timeSpan = timeSpan.Subtract(new TimeSpan(0, 0, 0, 0, (int)(dt * 1000)));
                }
                if (timeSpan.CompareTo(TimeSpan.Zero) > 0)
                {
                    var s = "" + timeSpan.Hours;
                    if (timeSpan.Hours < 10)
                        s = "0" + s;
                    s += ":";
                    if (timeSpan.Minutes < 10)
                        s += "0" + timeSpan.Minutes;
                    else
                        s += timeSpan.Minutes;
                    s += ":";
                    if (timeSpan.Seconds < 10)
                        s += "0" + timeSpan.Seconds;
                    else
                        s += timeSpan.Seconds;
                    s += ":";

                    if (timeSpan.Milliseconds < 10)
                        s += "00" + timeSpan.Milliseconds;
                    else if (timeSpan.Milliseconds < 100)
                        s += "0" + timeSpan.Milliseconds;
                    else
                        s += timeSpan.Milliseconds;
                    simpleClock.text = s;
                }
                else
                {
                    if (originalTimerStatus != TimerStatus.Idle)
                    {
                        timerStatus = TimerStatus.Fired;
                        //pauseButton.enabled = false;
                        //resumeButton.enabled = false;
                        //startTimerButton.enabled = true;
                        timeSpan = originalTimeSpan;
                        simpleClock.text = "";
                        time_from_last_notify = NOTIFY_INTERVAL;
                    }
                }
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Game.Exit();
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();
            title.Draw(spriteBatch);

            if (timerStatus == TimerStatus.Fired)
            {
                add10minute.Draw(spriteBatch);
                add1minute.Draw(spriteBatch);
                add5minute.Draw(spriteBatch);
                ignore.Draw(spriteBatch);
                fired.Draw(spriteBatch);
            }
            else
            {
                stopButton.Draw(spriteBatch);
                resumeButton.Draw(spriteBatch);
                pauseButton.Draw(spriteBatch);
                newTimerButton.Draw(spriteBatch);
                creditsButton.Draw(spriteBatch);
                settingsButton.Draw(spriteBatch);
                startTimerButton.Draw(spriteBatch);
                simpleClock.Draw(spriteBatch);
                
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            if (timerStatus == TimerStatus.Fired)
            {
                if (ignore.tapped_on(tap_position))
                {
                    timeSpan = originalTimeSpan;
                    pauseButton.enabled = false;
                    resumeButton.enabled = false;
                    startTimerButton.enabled = true;
                    stopButton.enabled = false;
                    newTimerButton.enabled = true;
                    timerStatus = TimerStatus.Idle;
                }
                else if (add1minute.tapped_on(tap_position))
                {
                    timeSpan = new TimeSpan(0, 1, 0);
                    timerStatus = TimerStatus.RunningDown;
                }
                else if (add5minute.tapped_on(tap_position))
                {
                    timeSpan = new TimeSpan(0, 5, 0);
                    timerStatus = TimerStatus.RunningDown;
                }
                else if (add10minute.tapped_on(tap_position))
                {
                    timeSpan = new TimeSpan(0, 10, 0);
                    timerStatus = TimerStatus.RunningDown;
                }
            }
            else
            {
                if (creditsButton.tapped_on(tap_position))
                {
                    if (timerStatus == TimerStatus.RunningDown || timerStatus == TimerStatus.RunningUp)
                    {
                        pauseButton.enabled = false;
                        resumeButton.enabled = true;
                    }
                    StateManager.SelectCreditsMenu();
                }
                else if (settingsButton.tapped_on(tap_position))
                {
                    if (timerStatus == TimerStatus.RunningDown || timerStatus == TimerStatus.RunningUp)
                    {
                        pauseButton.enabled = false;
                        resumeButton.enabled = true;
                    }
                    StateManager.SelectOptionsMenu();
                }
                else if (newTimerButton.tapped_on(tap_position))
                {
                    StateManager.SelectSetTimerMenu();
                }
                else if (stopButton.tapped_on(tap_position))
                {
                    timerStatus = TimerStatus.Stopped;
                    stopButton.enabled = false;
                    newTimerButton.enabled = true;
                    pauseButton.enabled = false;
                    resumeButton.enabled = false;
                    timeSpan = originalTimeSpan;
                    startTimerButton.enabled = true;
                }
                else if (pauseButton.tapped_on(tap_position))
                {
                    timerStatus = TimerStatus.Paused;
                    resumeButton.enabled = true;
                    pauseButton.enabled = false;
                }
                else if (resumeButton.tapped_on(tap_position))
                {
                    timerStatus = originalTimerStatus;
                    resumeButton.enabled = false;
                    pauseButton.enabled = true;
                }
                else if (startTimerButton.tapped_on(tap_position))
                {
                    startTimerButton.enabled = false;
                    stopButton.enabled = true;
                    pauseButton.enabled = true;
                    timerStatus = originalTimerStatus;
                    newTimerButton.enabled = false;
                }
            }
        }
    }
}
